package leetcode.Y2024M7;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class S22 {
    List<String> res = new ArrayList<>();

    public List<String> generateParenthesis(int n) {
        if (n <= 0) return res;
        dfs("", n, n);
        return res;
    }

    /**
     * 左右括号数都为3
     *
     * @param s 溯洄字符串
     * @param left 左括号数量
     * @param right 右括号数量
     */
    private void dfs(String s, int left, int right) {
        if (left == 0 && right == 0) {
            res.add(s);
            return;
        }
        if (left == right) {
            dfs(s + "(", left - 1, right);
        } else if (left < right) {
            if (left > 0) {
                dfs(s + "(", left - 1, right);
            }
            dfs(s + ")", left, right - 1);
        }
    }

    @Test
    public void test() {
        System.out.println(generateParenthesis(3));
    }
}

